package cn.wen.eeayjoke.ui;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.wen.base.adapter.recyclerview.QuickRecyclerAdapter;
import cn.wen.base.adapter.recyclerview.QuickRecyclerViewHolder;
import cn.wen.core.log.LogUtils;
import cn.wen.core.titlebar.DefTitleBar;
import cn.wen.core.ui.AppActivity;
import cn.wen.eeayjoke.R;
import cn.wen.image.ImageHelper;
import cn.wen.image.OnImageListener;
import cn.wen.image.PreviewActivity;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 图片选择测试的Activity
 */
public class SelectedImageTestActivity extends AppActivity {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    private ImageHelper helper;
    private ArrayList<String> mImages = new ArrayList<>();
    private QuickRecyclerAdapter<String> mAdapter;

    @Override
    public Object getContentLayout() {
        return R.layout.activity_image_test;
    }

    @Override
    public void initView(View contentView) {
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mAdapter = new QuickRecyclerAdapter<String>(this, mImages, R.layout.item_test_image) {
            @Override
            protected void convert(QuickRecyclerViewHolder holder, String item, final int position) {
                holder.displayImage(R.id.square_image, item);
                holder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(PreviewActivity.POSITION, position);
                        bundle.putStringArrayList(ImageHelper.EXTRA_DEFAULT_SELECTED_LIST, mImages);
                        startActivity(PreviewActivity.class, bundle);
                    }
                });
            }
        };
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 打开图片选择
     */
    public void open(View view) {
        helper = new ImageHelper(this);
        helper.origin(mImages);
        helper.start(new OnImageListener() {
            @Override
            public void onImageSelected(ArrayList<String> images) {
                mImages = images;
                mAdapter.clear();
                // 图片裁剪

                // mAdapter.addAll(mImages);
                // compressImage(images);
            }
        });

    }

    private void compressImage(ArrayList<String> images) {

        Observable.fromArray(images)
                .map(new Function<ArrayList<String>, List<String>>() {
                    @Override
                    public List<String> apply(ArrayList<String> srcPaths) throws Exception {
                        List<String> paths = new ArrayList<>();
                        for (String srcPath : srcPaths) {
                            //String outPath = FileUtil.createFilePath(getAppActivity()).getAbsolutePath();
                            // 压缩图片
                            //ImageCompressUtil.compress(getAppActivity(), srcPath, outPath, 40);
                            //paths.add(outPath);
                        }
                        return paths;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<String>>() {
                    @Override
                    public void accept(List<String> bitmaps) throws Exception {
                        LogUtils.e("------------>>" + bitmaps.size());
                    }
                });
    }


    @Override
    protected void initTitle(DefTitleBar titleBar) {
        titleBar.setTitle("图片选择测试");
    }

}
